using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.Hosting;
using Microsoft.MediaCenter.UI;
using System.Threading;
using MediaCenterCommander.Code;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Collections;
using System;

namespace MediaCenterCommander
{
    public class Application : ModelItem
    {
        private AddInHost host;        

        private MediaCenterActionQueue actionQueue;
        private MediaCenterController mediaCenterController;


        public Application(AddInHost host)
        {
            this.host = host;            
        }

        public MediaCenterEnvironment MediaCenterEnvironment
        {
            get
            {
                if (host == null) return null;
                return host.MediaCenterEnvironment;
            }
        }                

        public void Start()
        {           
            // TODO: Allow the port number to be configured.
            IDictionary props = new Hashtable();
            props["name"] = "tcp4";
            props["port"] = 9901;
            props["bindTo"] = "127.0.0.1";
            TcpChannel tcpChannel = new TcpChannel(props, null, null);
            ChannelServices.RegisterChannel(tcpChannel, false);

            this.actionQueue = new MediaCenterActionQueue();

            this.mediaCenterController = new MediaCenterController(this.host, this.actionQueue);
            RemotingServices.Marshal(this.mediaCenterController, "IMediaCenterController");

            var uris = tcpChannel.GetUrlsForUri("IMediaCenterController");
            string uri = uris[0];
            
            using (var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\MediaCenterCommander"))
            {
                key.SetValue("IMediaCenterController", uri);
            }

            for (; ; )
            {
                var action = this.actionQueue.WaitForNextAction();
                try
                {
                    action();
                }
                catch
                {
                }
            }
        }


        public void DialogTest(string strClickedText)
        {
            int timeout = 5;
            bool modal = true;
            string caption = Resources.DialogCaption;

            if (host != null)
            {
                MediaCenterEnvironment.Dialog(strClickedText,
                                              caption,
                                              new object[] { DialogButtons.Ok },
                                              timeout,
                                              modal,
                                              null,
                                              delegate(DialogResult dialogResult) { });
            }
            else
            {
                Debug.WriteLine("DialogTest");
            }
        }
    }
}